package cn.tqfeiyang.springplus.warehouse.controller;

import cn.tqfeiyang.springplus.warehouse.service.CustomerService;
import cn.tqfeiyang.springplus.warehouse.request.CustomerDTO;
import cn.tqfeiyang.springplus.warehouse.response.CustomerVO;
import cn.tqfeiyang.springplus.warehouse.request.CustomerSTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.springplus.framework.response.ApiResult;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import cn.tqfeiyang.springplus.framework.validation.WhenCreate;
import cn.tqfeiyang.springplus.framework.validation.WhenUpdate;
import cn.tqfeiyang.springplus.framework.validation.EntityId;
import cn.tqfeiyang.springplus.framework.security.JwtSecurityRequirement;
import cn.tqfeiyang.springplus.framework.logging.LoggingRequirement;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

/**
 * 客户前端控制器
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/api/warehouse/customer")
@Tag(name = "基础/客户")
public class CustomerController {

    /**
     * 客户服务
     */
    @Resource
    private CustomerService customerService;

    /**
     * 查询客户
     *
     * @param customerSTO 查询数据
     * @return 客户列表
     */
    @GetMapping("/query")
    @Operation(summary = "查询客户")
    @PreAuthorize("hasAnyAuthority('warehouse:customer:query')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<PageData<CustomerVO>> selectCustomerPage(CustomerSTO customerSTO) {
        return ApiResult.success(customerService.selectCustomerPage(customerSTO));
    }

    /**
     * 读取客户
     *
     * @param companyId 客户id
     * @return 客户信息
     */
    @GetMapping("/read")
    @Operation(summary = "读取客户")
    @PreAuthorize("hasAnyAuthority('warehouse:customer:read')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<CustomerVO> selectCustomerInfo(@RequestParam @EntityId Long companyId) {
        return ApiResult.success(customerService.selectCustomerInfo(companyId));
    }

    /**
     * 新建客户
     *
     * @param customerDTO 请求数据
     * @return 客户信息
     */
    @PostMapping("/create")
    @Operation(summary = "新建客户")
    @PreAuthorize("hasAnyAuthority('warehouse:customer:create')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<CustomerVO> createCustomer(@RequestBody @Validated(WhenCreate.class) CustomerDTO customerDTO) {
        Long companyId = customerService.createCustomer(customerDTO);
        return ApiResult.success(customerService.selectCustomerInfo(companyId));
    }

    /**
     * 修改客户
     *
     * @param companyId 客户id
     * @param customerDTO 请求数据
     * @return 客户信息
     */
    @PostMapping("/update")
    @Operation(summary = "修改客户")
    @PreAuthorize("hasAnyAuthority('warehouse:customer:update')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult<CustomerVO> updateCustomer(@RequestParam @EntityId Long companyId, @RequestBody @Validated(WhenUpdate.class) CustomerDTO customerDTO) {
        customerService.updateCustomer(companyId, customerDTO);
        return ApiResult.success(customerService.selectCustomerInfo(companyId));
    }

    /**
     * 删除客户
     *
     * @param companyId 客户id
     */
    @PostMapping("/delete")
    @Operation(summary = "删除客户")
    @PreAuthorize("hasAnyAuthority('warehouse:customer:delete')")
    @JwtSecurityRequirement
    @LoggingRequirement
    public ApiResult deleteCustomer(@RequestParam @EntityId Long companyId) {
        customerService.deleteCustomer(companyId);
        return ApiResult.success();
    }
}
